<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/4
 * Time: 17:07
 */

namespace Home\Controller;


use Common\Controller\HomeController;
use Common\Model\GoodsListModel;
use Common\Model\OrderModel;

class OrderController extends HomeController
{
	public function index(){
		//分配公共头部数据
		$conf = ['js'=>['list','account','area'],
			'css'=>['account']
		];
		$this->assign('conf',$conf);

		$data = $_SESSION['order'];
		foreach ($data['order'] as $k=>$v){
			$data['order'][$k]['pic'] = (new GoodsListModel())->alias('gl')->join("ccshop_goods g on gl.gid=g.gid")->where("gl.glid={$v['id']}")->getField('pic');

		}
//		dd($data);die;
		$address = (new OrderModel())->where("uid={$_SESSION['uid']}")->select();
//        dd($address);
		$this->assign('data',$data);
		$this->assign('address',$address);
		$this->display();
	}
	/**
	 * 购物车页面提交过来的订单数据
	 */
	public function getCartData()
	{
		if(IS_POST){
//			dd($_POST);die;
			if(!isset($_POST['sid'])){
				$this->error('请选择下单的商品');exit;
			}
			$sids = $_POST['sid'];//购物车提交过来的要下单的sid数据
			$data = [];
			foreach($sids as $k=>$sid){
				$data['order'][$sid] = $_SESSION['cart']['goods'][$sid];
			}
//			dd($_SESSION);
//			dd($data);die;
			$data['total'] = 0;//所有订单总价
			$data['total_rows'] = 0;//所有订单总条数

			foreach($data['order'] as $k=>$v){
				$data['total'] = $data['total']+$v['total'];
				$data['total_rows'] = $data['total_rows']+$v['num'];
			}
//			dd($data);die;
			//将订单数据存入到session中
			$_SESSION['order'] = $data;
//			dd($_SESSION);
//			dd($data);die;
			redirect(u('index'));exit;
		}else{
			//跳转到购物车页面
			redirect(u('Home/Cart/index'));exit;
		}
	}

	/**
	 * 创建新地址
	 */
	public function newAddress(){
		if (IS_POST){
			$data =I('post.');
			$data['address'] =implode(' ',$data['areal']).' '.$data['postcode'];
			$data = [
				'consignee'      =>  $data['name'],
				'address'        =>  implode(' ',$data['areal']).' '.$data['particular'],
				'mobile'         =>  $data['shouji'],
				'uid'            =>  $_SESSION['uid'],
				'time'           =>  time(),
			];
//			dd($data);
			(new OrderModel())->add($data);
			redirect(u('index'));exit;
		}
	}

	/**
	 * 修改地址
	 */
	public function editAddress(){
		if (IS_POST){
			$data =I('post.');
//			dd($data);die;
			$data['address'] =implode(' ',$data['areal']).' '.$data['postcode'];
			$data = [
				'oid'            =>  $data['oid'],
				'consignee'      =>  $data['name'],
				'address'        =>  $data['particular'],
				'mobile'         =>  $data['shouji'],
				'uid'            =>  $_SESSION['uid'],
				'time'           =>  time(),
			];
//			dd($data);die;
			(new OrderModel())->where("oid={$data['oid']}")->save($data);
			redirect(u('index'));exit;
		}
	}

	/**
	 * 异步获取选中地址
	 */
	public function ajaxAddress(){
		if (IS_AJAX){
//			dd(trim($_POST['conten']));
			$content = explode('，',trim($_POST['conten']));
//			dd($content);
			echo json_encode(['name'=>$content[0],'address'=>$content[1],'mobile'=>$content[2]]);
			exit;
		}
	}

}